<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="菜单名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="菜单名称"></el-input>
    </el-form-item>
    <el-form-item label="菜单级别" prop="menuLevel">
      <el-input v-model="dataForm.menuLevel" placeholder="菜单级别"></el-input>
    </el-form-item>
    <el-form-item label="简介" prop="summary">
      <el-input v-model="dataForm.summary" placeholder="简介"></el-input>
    </el-form-item>
    <el-form-item label="父uid" prop="parentUid">
      <el-input v-model="dataForm.parentUid" placeholder="父uid"></el-input>
    </el-form-item>
    <el-form-item label="url地址" prop="url">
      <el-input v-model="dataForm.url" placeholder="url地址"></el-input>
    </el-form-item>
    <el-form-item label="图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="图标"></el-input>
    </el-form-item>
    <el-form-item label="排序字段，越大越靠前" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段，越大越靠前"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否显示 1:是 0:否" prop="isShow">
      <el-input v-model="dataForm.isShow" placeholder="是否显示 1:是 0:否"></el-input>
    </el-form-item>
    <el-form-item label="菜单类型 0: 菜单   1: 按钮" prop="menuType">
      <el-input v-model="dataForm.menuType" placeholder="菜单类型 0: 菜单   1: 按钮"></el-input>
    </el-form-item>
    <el-form-item label="是否跳转外部链接 0：否，1：是" prop="isJumpExternalUrl">
      <el-input v-model="dataForm.isJumpExternalUrl" placeholder="是否跳转外部链接 0：否，1：是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          name: '',
          menuLevel: '',
          summary: '',
          parentUid: '',
          url: '',
          icon: '',
          sort: '',
          status: '',
          createTime: '',
          updateTime: '',
          isShow: '',
          menuType: '',
          isJumpExternalUrl: ''
        },
        dataRule: {
          name: [
            { required: true, message: '菜单名称不能为空', trigger: 'blur' }
          ],
          menuLevel: [
            { required: true, message: '菜单级别不能为空', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '简介不能为空', trigger: 'blur' }
          ],
          parentUid: [
            { required: true, message: '父uid不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: 'url地址不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '图标不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段，越大越靠前不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isShow: [
            { required: true, message: '是否显示 1:是 0:否不能为空', trigger: 'blur' }
          ],
          menuType: [
            { required: true, message: '菜单类型 0: 菜单   1: 按钮不能为空', trigger: 'blur' }
          ],
          isJumpExternalUrl: [
            { required: true, message: '是否跳转外部链接 0：否，1：是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/categorymenu/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.categoryMenu.name
                this.dataForm.menuLevel = data.categoryMenu.menuLevel
                this.dataForm.summary = data.categoryMenu.summary
                this.dataForm.parentUid = data.categoryMenu.parentUid
                this.dataForm.url = data.categoryMenu.url
                this.dataForm.icon = data.categoryMenu.icon
                this.dataForm.sort = data.categoryMenu.sort
                this.dataForm.status = data.categoryMenu.status
                this.dataForm.createTime = data.categoryMenu.createTime
                this.dataForm.updateTime = data.categoryMenu.updateTime
                this.dataForm.isShow = data.categoryMenu.isShow
                this.dataForm.menuType = data.categoryMenu.menuType
                this.dataForm.isJumpExternalUrl = data.categoryMenu.isJumpExternalUrl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/categorymenu/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'name': this.dataForm.name,
                'menuLevel': this.dataForm.menuLevel,
                'summary': this.dataForm.summary,
                'parentUid': this.dataForm.parentUid,
                'url': this.dataForm.url,
                'icon': this.dataForm.icon,
                'sort': this.dataForm.sort,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isShow': this.dataForm.isShow,
                'menuType': this.dataForm.menuType,
                'isJumpExternalUrl': this.dataForm.isJumpExternalUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
